$:.unshift File.join(File.dirname(__FILE__), "..","..","m")

require 'test/unit'
require 'datainform'

class InformerTest < Test::Unit::TestCase

	class Warner
		
		def initialize(infor)
			infor.add_observer(self)		  
		end
		def update(infor)
			puts "\nInformer change-> STATE #{infor.state?.to_s}, NOTICE #{infor.notice?.last.to_s}\n"
		end
	end

	def setup
		puts "\n\n------------------- SETUP --------\n\n"
		@bool 			= BoolInform.new
		@wbool 			= Warner.new(@bool)
		@word 			= WordInform.new
		@wtext 			= Warner.new(@word)
		@hostname	= HostnameInform.new
		@whostname = Warner.new(@hostname)
	end       

	def teardown
	end

	def test_dataOk
		puts "\n\n------------------- TEST DATA OK --------\n\n"

		[true, false, 5==(4+1), 5==(3+1), z=(2==(1+1)), eval('true')].each do |value|
			@bool.data= value
			assert_equal(value, @bool.data?)
			assert_equal(Informer::OK, @bool.state?)
		end

		['test10', '10TEST_', 'zanknan', 'class_this', '_NaN_'].each do |value|
			@word.data= value
			assert_equal(value, @word.data?)
			assert_equal(Informer::OK, @word.state?)
		end

		['localhost', '127.0.0.1', 'www.yahoo.es',
		 'gmail.com', 'pop.mail.foo.bar.com'].each do |value|
			@hostname.data= value
			assert_equal(value, @hostname.data?)
			assert_equal(Informer::OK, @hostname.state?)
		end		
	end

	def test_dataERROR
		puts "\n\n------------------- TEST DATA ERROR --------\n\n"

		[5, nil, z=(3+1), 'not', '5==(4+1)', 'eval(true)', Proc.new {return true}].each do |value|
			@bool.data= value
			assert_equal(nil, @bool.data?)
			assert_equal(Informer::ERROR, @bool.state?)
		end

		['te.st10', '10 TEST_', '\n', "\n", "te \n st", "<>"].each do |value|
			@word.data= value
			assert_equal(nil, @word.data?)
			assert_equal(Informer::ERROR, @word.state?)
		end

		['www. yahoo.es', '-- gmail.com', 'p+op.mail.foobar.com', 
		 '327.0.0.0', '256.0.0.0', 'mail.zoo.org:567',
		 'mailto:lion@mail.zoo.org?Subject=text'].each do |value|
			@hostname.data= value
			assert_equal(nil, @hostname.data?)
			assert_equal(Informer::ERROR, @hostname.state?)
		end				
	end

	def test_dataWARN
		puts "\n\n------------------- TEST DATA WARN --------\n\n"

		['http://mail.zoo.org', 'ftp://localhost', 
		 'ldap://127.0.0.1/ES'].each do |value|
			@hostname.data= value
			assert(value.include?(@hostname.data?))
			assert_equal(Informer::WARN, @hostname.state?)
		end				
	end

end
